<?php
require_once ("db/db_connector.php");
require_once ("db/db_paradise.php");
require_once ("db/db_tag.php");
require_once ("db/db_picture.php");

// Database Access Begins ---------------------------------------- //
$con = get_db_con();

// Database Access Ends ------------------------------------------ //
?>

<?php
include_once ("include/header.php");
?>



<?php
session_start();
$tag_list = $_SESSION['tag_list'];
$paradise_list = $_SESSION['paradise_list'];
$new_value = $_POST['tag_names'];
$search_words = array();
if (isset($_SESSION['search_words'])) {
	$list = $_SESSION['search_words'];
	foreach ($list as $key) {
		array_push($search_words, $key);
	}
}
foreach ($new_value as $key) {
	array_push($search_words, $key);
}
$_SESSION["search_words"] = $search_words;
$new_list = get_Relevant_Paradise_List($paradise_list, $search_words, $tag_list);
$paradise_id_list = array();

foreach ($new_list as $paradise) {
	array_push($paradise_id_list, $paradise['id']);
}

$temp_list = get_Associated_Tags($paradise_id_list, $con);

$new_tags = array();
foreach ($temp_list as $tag) {
	if (!in_array($tag['name'], $search_words)) {
		array_push($new_tags, $tag['name']);
	}

}

function get_Relevant_Paradise_List($paradise_list, $search_words, $tag_list) {
	//$tag_list contains all tags not just distinct tags
	$paradise_id_list = array();
	//computes the scores of each paradise
	foreach ($tag_list as $tag) {
		if (in_array($tag["name"], $search_words)){
			if (isset( $paradise_id_list[$tag["paradise_id"]]))
				$paradise_id_list[$tag["paradise_id"]] += 1;
			else
				$paradise_id_list[$tag["paradise_id"]] = 1;
        }
	}
	//echo $paradise_id_list[8];
	//check the scores of the paradise if score=count(search_words)
	


	$new_paradise_list = array();
	foreach ($paradise_list as $paradise) {
		if (in_array($paradise["id"], array_keys($paradise_id_list))) {

			if ($paradise_id_list[$paradise["id"]] == count($search_words)) {
				array_push($new_paradise_list, $paradise);
			}
		}
	}
	//new paradise list contains matching paradise

	return $new_paradise_list;
}

function cmp($a, $b) {
	return (strcmp($a["name"], $b["name"]));
}

function get_Associated_Tags($paradise_id_list, $con) {
	$str = "";
	foreach ($paradise_id_list as $paradise_id) {
		$str = $str . "'$paradise_id',";
	}
	$str = substr($str, 0, -1);
	return get_all_tags_for_paradise($con, $str);
	//array_push($paradise_id_list, $paradise["id"])

}
?>

<div style="float: left">
	
	<form method="post" action="search_result.php">
		<? foreach ($new_tags as $tag) :?>
		<input type="checkbox" name="tag_names[]" value=<?=$tag ?> onclick="this.form.submit()" ><?=$tag ?></input>
		</br>		
		<?endforeach ?>
	</form>
	
</div>
<div style="float: left">
	<ul>
	<?foreach($search_words as $tag):?>
	<li><?=$tag ?></li>
	
	<?endforeach ?>
	</ul>

</div>
<div style="float:right">
	<h2>Paradise Over the World</h2>
	<table>
		<tr>
			<th>thumb_small</th>
			<th>name</th>
		</tr>
		<?php foreach ($new_list as $paradise):
		?>
		<tr>
			<td><?php
			if (is_null($paradise["picture_id"]))
				echo "NO ASSIGNED IMAGE";
			else {
				$con = get_db_con();
				$image = get_picture($con, $paradise["picture_id"]);
				echo "<img src=" . $image["thumb_small"] . "/>";
				close_db_con();
			}
			?></td>
			<td><a href="paradise.php?id=<?=$paradise["id"] ?>"> <?=$paradise["name"] ?></a></td>

		</tr>
		<?php endforeach ?>
	</table>
</div>